import Vue from 'vue'
import VueRouter from 'vue-router'

import MyLogin from '@/components/MyLogin.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    component: MyLogin
  },
  {
    path: '/home',
    component: () => import('@/components/MyHome.vue'),
    children: [{
        path: 'goods',
        component: () => import('@/components/menus/MyGoods.vue')
      },
      {
        path: 'orders',
        component: () => import('@/components/menus/MyOrders.vue')
      },
      {
        path: 'rights',
        component: () => import('@/components/menus/MyRights.vue')
      },
      {
        path: 'settings',
        component: () => import('@/components/menus/MySettings.vue')
      },
      {
        path: 'users',
        component: () => import('@/components/menus/MyUsers.vue')
      },
      {
        path: 'detail/:id',
        component: () => import('@/components/user/MyUserDetail.vue')
      },
    ]
  },
  { path: '*', component: () => import('@/components/NotFound.vue') }

]

const router = new VueRouter({
  mode:'history',
  linkActiveClass: 'aa',
  linkExactActiveClass: 'bb',
  routes
})

// 全局前置导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next() // 访问登录页，直接放行
  } else {
    // 访问其他页，需要判断本地存储是否有token
    if (localStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router